<?php

namespace App\Admin\Http\Controllers\Web;

use App\Admin\Eloquent\FriendlyLink;
use App\Admin\Http\Controllers\Controller;
use App\Admin\Http\Requests\FriendlyLinkRequest;
use App\Exceptions\BusinessException;
use Illuminate\Http\Request;
use Laracasts\Flash\Flash;

class FriendlyLinksController extends Controller
{
    /**
     * 友情链接列表页面
     *
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        $query = $request->only('name', 'link_type');

        $link_types = FriendlyLink::get_types();

        $friendly_links = FriendlyLink::paging_query($query);


        $links = $friendly_links->render();

        return view('admin.web.friendly_links.index', compact('query', 'link_types', 'friendly_links', 'links'));
    }

    /**
     * 返回创建友情链接表单
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create()
    {
        $friendly_link_types = FriendlyLink::get_types();

        return view('admin.web.friendly_links.create', compact('friendly_link_types'));
    }

    /**
     * 保存一条友情链接
     *
     * @param FriendlyLinkRequest $request
     * @return \Illuminate\Http\RedirectResponse
     * @throws BusinessException
     */
    public function store(FriendlyLinkRequest $request)
    {
        $friendly_link = new FriendlyLink($request->input());

        if ($image = $request->file('image')) {
            $json = upload_file($image);    //上传文件
            if ($json['code'] == 10000) {
                $friendly_link->image = "{$json['file']['full_url']}";
            } else {
                throw new BusinessException('文件上传失败:' . $json['message']);
            }
        }

        $friendly_link->save();

        Flash::success('新增友情链接记录成功!');
        return $this->redirectAction('Web\FriendlyLinksController@index');
    }

    /**
     * 修改友情链接表单
     *
     * @param int $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit(int $id)
    {
        $friendly_link = FriendlyLink::findOrFail($id);

        $link_types = FriendlyLink::get_types();

        return view('admin.web.friendly_links.edit', compact('friendly_link', 'link_types'));
    }

    /**
     * 修改友情链接记录
     *
     * @param FriendlyLinkRequest $request
     * @param int $id
     * @return \Illuminate\Http\RedirectResponse
     * @throws BusinessException
     */
    public function update(FriendlyLinkRequest $request, int $id)
    {
        $args = $request->input();

        $friendly_link = FriendlyLink::findOrFail($id);

        if ($image = $request->file('image')) {
            $json = upload_file($image);    //上传文件
            if ($json['code'] == 10000) {
                $friendly_link->image = "{$json['file']['full_url']}";
            } else {
                throw new BusinessException('文件上传失败:' . $json['message']);
            }
        } else {
            //处理移除图片,如果存在这个参数,说明用户移除了图片
            if ($image = $request->input('image')) {
                $arr['image'] = $image;
            }
        }

        $friendly_link->update($args);

        Flash::success('修改友情链接记录成功!');

        return $this->redirectAction('Web\FriendlyLinksController@index');
    }

    /**
     * 根据id删除友情链接记录
     *
     * @param int $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function destroy(int $id){

        $friendly_link = FriendlyLink::findOrFail($id)->delete();

        Flash::success('删除友情链接记录成功!');

        return $this->redirectAction('Web\FriendlyLinksController@index');
    }

    /**
     * 查看友情链接详情
     *
     * @param int $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function show(int $id){
        $friendly_link = FriendlyLink::findOrFail($id);

        return view('admin.web.friendly_links.show',compact('friendly_link'));
    }
}